<script lang="ts" setup>
    import { reactive } from 'vue'
    import type { UploadProps } from 'element-plus'
    import JsonEditorVue from 'json-editor-vue3';
    const form = reactive({
        name: '',
        imgUrl:'',
        code:'const a = "3245"',
        jsonEditFlag:true
    })
    const jsonEditor = ref();
    let code = ref({})
    async function jsonValidate () {
        if (jsonEditor.value) {
            const res = await jsonEditor.value.editor.validate();
            if(res.length != 0){
                form.jsonEditFlag = false;
            }else {
                form.jsonEditFlag = true;
            }
        }
    }
    const handleAvatarSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
        console.log(response,'up')
        form.imgUrl = URL.createObjectURL(uploadFile.raw!)
    }
    const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
        if (!['image/png','image/jpeg'].includes(rawFile.type)) {
            ElMessage.error('Avatar picture must be JPG format!')
            return false
        } else if (rawFile.size / 1024 / 1024 > 2) {
            ElMessage.error('Avatar picture size can not exceed 2MB!')
            return false
        }
        return true
    }
</script>
<style lang="scss">
    .addForm{
        padding: 0 10%;
        .avatar-uploader .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
        .avatar-uploader .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .avatar-uploader .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
</style>
<template>
    <div class="addForm">
        <el-form :model="form" label-width="120px">
            <el-form-item label="组件名称">
                <el-input v-model="form.name" />
            </el-form-item>
            <el-form-item label="图片">
                <el-upload
                        class="avatar-uploader"
                        action=""
                        :show-file-list="false"
                        :onSuccess="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload"
                >
                    <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon">
                        <i-ep-plus />
                    </el-icon>
                </el-upload>
            </el-form-item>
            <el-form-item label="示例代码">
                <JsonEditorVue ref="jsonEditor" class="editor" v-model="code" @keyup="jsonValidate" />
            </el-form-item>
        </el-form>
    </div>
</template>
